﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CommonLibrary.Cookie
{
    public class MultiCookie
    {
        /// <summary>
        /// set cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="cookieValue">cookie value</param>
        /// <param name="cookieExpires">cookie expires time</param>
        public static void SetCookie(string cookieName, List<CookieEntity> cookieValues, CookieExpires cookieExpires)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(cookieName);
            }
            HttpCookie Cookies = new HttpCookie(cookieName);
            foreach (CookieEntity cookie in cookieValues)
            {
                Cookies.Values.Add(cookie.CookieName, HttpUtility.UrlEncode(cookie.CookieValue));
            }
            if (System.Configuration.ConfigurationManager.AppSettings["PageDomain"] != null)
            {
                Cookies.Domain = System.Configuration.ConfigurationManager.AppSettings["PageDomain"];
            }
            switch (cookieExpires)
            {
                case CookieExpires.Browser:
                    break;
                case CookieExpires.OneHour:
                    Cookies.Expires = DateTime.Now.AddHours(1);
                    break;
                case CookieExpires.OneDay:
                    Cookies.Expires = DateTime.Now.AddDays(1);
                    break;
                case CookieExpires.OneWeek:
                    Cookies.Expires = DateTime.Now.AddDays(7);
                    break;
                case CookieExpires.OneMonth:
                    Cookies.Expires = DateTime.Now.AddMonths(1);
                    break;
                case CookieExpires.OneYear:
                    Cookies.Expires = DateTime.Now.AddYears(1);
                    break;
            }
            HttpContext.Current.Response.Cookies.Add(Cookies);
        }


        /// <summary>
        /// get cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <returns>cookie value</returns>
        public static string GetCookieValue(string cookieName,string valueKey)
        {
            string cookieValue = "";
            HttpCookie cookie=HttpContext.Current.Request.Cookies[cookieName];
            if ( cookie!= null)
            {
                cookieValue = cookie.Values[valueKey];
            }
            return HttpUtility.UrlDecode( cookieValue);

        }

        /// <summary>
        /// remover cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        public static void RemoveCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(cookieName);
            }
            HttpCookie Cookies = new HttpCookie(cookieName);
            Cookies.Value = "novalue";
            if (System.Configuration.ConfigurationManager.AppSettings["PageDomain"] != null)
            {
                Cookies.Domain = System.Configuration.ConfigurationManager.AppSettings["PageDomain"];
            }
            Cookies.Expires = DateTime.Now.AddDays(-7);
            HttpContext.Current.Response.Cookies.Add(Cookies);
        }


    }
}
